// packageA/pages/orderService/index.js
const dayjs = require("dayjs");
const WXAPI = require("../../../api/api");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    serviceObj: {},
    show: false,
    minHour: 10,
    maxHour: 20,
    minDate: new Date().getTime(),
    maxDate: new Date(2040, 10, 1).getTime(),
    currentDate: new Date().getTime(),
    currentShowDate: "",
    currentAddress: {
      sId: "",
    },
  },
  async toOrderService() {
    if (!this.data.currentAddress.sId) {
      wx.showToast({
        title: "请选择上门服务地址！",
        icon: "none",
      });
      return false;
    }
    if (!this.data.currentShowDate) {
      wx.showToast({
        title: "请选择服务时间！",
        icon: "none",
      });
      return false;
    }
    let data = {
      sAddressId: this.data.currentAddress.sId,
      dServiceTime: this.data.currentShowDate,
      Projects: [
        {
          sProjectId: this.data.serviceObj.sProjectId,
          iCount: 1,
        },
      ],
    };
    let res = await WXAPI.submitServoceOrder(data);
    if (res.data.code == 0) {
      wx.showToast({
        title: res.data.message,
      });
      setTimeout(() => {
        wx.redirectTo({
          url: `/packageA/pages/all-order/index?orderType=2`,
        });
      }, 2000);
    }
  },
  toSelectAddress() {
    wx.navigateTo({
      url: `/packageA/pages/address-manage/index?isSelectAddress=${true}`,
    });
  },
  onClose() {
    this.setData({
      show: false,
    });
  },
  selectDateTime() {
    this.setData({
      show: true,
    });
  },
  cancel() {
    this.setData({
      show: false,
    });
  },
  onInput(event) {},

  confirm(event) {
    this.setData({
      currentDate: event.detail,
      currentShowDate: dayjs(event.detail).format("YYYY-MM-DD HH:mm:ss"),
    });
    this.setData({
      show: false,
    });
  },
  // 获取默认地址
  async getDefaultAddress() {
    let res = await WXAPI.getMyAddressList(this.data.search);
    if (res.data.code == 0 && res.data.data.list.length > 0) {
      let arr = res.data.data.list.filter(
        (item) => item.iDefaultAddress == 1
      )[0];
      this.setData({
        currentAddress: arr && arr.iDefaultAddress == "1" ? arr : {},
      });
    }
  },
  // 获取选择的地址
  async getAddressById(id) {
    let res = await WXAPI.getAddressInfo(id);
    if (res.data.code == 0) {
      this.setData({
        currentAddress: res.data.data,
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.serviceObj) {
      this.setData({
        serviceObj: JSON.parse(options.serviceObj),
      });
    }
    this.getDefaultAddress();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 选择地址返回当前页存储id
    if (this.data.addressId) {
      this.getAddressById(this.data.addressId);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
